package com.example.train_operate.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.train_operate.entity.Instruction;
import com.example.train_operate.mapper.InstructionMapper;
import com.example.train_operate.service.InstructionService;
import com.example.train_operate.util.Result;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import java.util.List;


/**
 * @author 21609
 * @description 针对表【tb_instruction】的数据库操作Service实现
 * @createDate 2025-08-13 17:06:39
 */
@Service
public class InstructionServiceImpl extends ServiceImpl<InstructionMapper, Instruction>
        implements InstructionService {
    @Resource
    private InstructionMapper instructionMapper;

    @Override
    public Result addInstruction(Instruction instruction) {
        try{
            if(instruction.getFaultDescription()==null || instruction.getFaultDescription().isEmpty()){
                return Result.fail().message("请填写故障描述");
            }
            if(instruction.getCauseAnalysis()==null || instruction.getCauseAnalysis().isEmpty()){
                return Result.fail().message("请填写原因分析");
            }
            if(instruction.getStep()==null || instruction.getStep().isEmpty()){
                return Result.fail().message("请填写详细步骤");
            }
            instructionMapper.insert(instruction);
            return Result.ok(instruction).message("添加成功");
        }
        catch (Exception e){
            System.out.println(e.getMessage());
            return Result.fail().message("出错了");
        }
    }

    @Override
    public Result updateInstruction(Instruction instruction) {
        if(instructionMapper.selectById(instruction.getFaultId())==null)
        {
            return Result.fail().message("没有此条指导书");
        }
        instructionMapper.updateById(instruction);
        return Result.ok().message("修改成功");
    }

    @Override
    public Result deleteInstruction(Integer instructionId) {
        if(instructionMapper.selectById(instructionId)==null)
        {
            return Result.fail().message("没有此条指导书");
        }
        instructionMapper.deleteById(instructionId);
        return Result.ok().message("删除成功");
    }

    @Override
    public Result getInstructionById(Integer instructionId) {
        Instruction instruction = instructionMapper.selectById(instructionId);
        if(instruction == null){
            return Result.fail().message("没有此条指导书");
        }
        return Result.ok(instruction);
    }

    //获取所有指导书
    @Override
    public List<Instruction> list(){
        return instructionMapper.list();
    }
}
